-- UIGDOpenActivity
-- Created by chengb May/7/2016
-- GD开启活动

UIGDOpenActivity = class("UIGDOpenActivity", function()
    return cc.Layer:create();
end);

function UIGDOpenActivity.create()
    return UIGDOpenActivity.new();
end

-- 输入框最大数量
local MAX_INPUT_NUM = 4;

-- 待输入的字段
local fieldMemoList = {};

local MIN_ACTIVITY_DURATION  = 1                   -- 活动持续时间下限（秒）
local MAX_ACTIVITY_DURATION  = 86400 * 30          -- 活动持续时间上限（秒）

-- 构造函数
function UIGDOpenActivity:ctor()
    self:setName("UIGDOpenActivity");
    local node = cc.CSLoader:createNode("layout/GD/ContentEdit.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 100000);

    fieldMemoList = {
        {["memo"] = getLocStr("activity_id_edit"), ["placeholder"] = "", },
        {["memo"] = getLocStr("start_time_edit"), ["placeholder"] = getLocStr("start_time_placeholder"), },
        {["memo"] = getLocStr("duration_time_edit"), ["placeholder"] = getLocStr("duration_placeholder"), },
    };

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIGDOpenActivity:registerTouchEvent()
    -- 确定
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:tryOpenActivity();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_open"));

    -- 返回
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIGDOpenActivity");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 尝试开启活动
function UIGDOpenActivity:tryOpenActivity()
    local id = self.editList[1]:getText();
    local startTime = self.editList[2]:getText();
    local duration = self.editList[3]:getText();

    -- 1.检查活动编号
    if string.len(id) == 0 then
        alert(getLocStr("please_input_activity_id"));
        return;
    end

    id = tonumber(id);
    if not ActivityM.query(id) then
        -- 活动编号错误
        alert(getLocStr("activity_id_error"));
        return;
    end

    -- 2.检查活动开始时间
    if string.len(startTime) == 0 then
        alert(getLocStr("please_input_activity_start_time"));
        return;
    end

    local serverTime = TimeM.getServerTime();
    serverTime = tonumber(os.date("%Y%m%d%H%M%S", serverTime));
    if string.len(startTime) ~= 14  or tonumber(startTime) == 0 or
        tonumber(startTime) < serverTime then
        -- 活动开始时间错误
        alert(getLocStr("activity_start_time_error"));
        return;
    end

    -- 3.检查活动持续时间
    if string.len(duration) == 0 then
        alert(getLocStr("please_input_activity_duration"));
        return;
    end

    local duration = tonumber(duration);
    if duration < MIN_ACTIVITY_DURATION or duration > MAX_ACTIVITY_DURATION then
        -- 活动持续时间错误
        alert(getLocStr("activity_duration_error"));
        return;
    end

    -- 开启活动
    GDM.openActivity(id, startTime, duration);
end

-- 重绘
function UIGDOpenActivity:redraw()
    local fieldNum = #fieldMemoList;
    self.editList = {};
    for i = 1, MAX_INPUT_NUM do
        local fieldNode = findChildByName(self.node, "CT/field" .. i);
        local nameLabel = findChildByName(fieldNode, "fieldname");
        local inputEdit = findChildByName(fieldNode, "field_input");

        if i > fieldNum then
            fieldNode:setVisible(false);
        else
            fieldNode:setVisible(true);

            local info = fieldMemoList[i]
            local memo = info.memo;
            local placeholder = info.placeholder;
            TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL , TextStyleM.TEXT_COLOR_BRIGHT_YELLOW, true);
            nameLabel:setString(memo);

            -- 设置编辑框
            local editBoxSize1 = inputEdit:getContentSize();
            local editBox = ccui.EditBox:create(editBoxSize1, "images/ui/login/1.png");
            editBox:setAnchorPoint(cc.p(0, 0));
            editBox:setPosition(inputEdit:getPosition());
            editBox:setFontName(getLocFontName());
            editBox:setFontSize(TextStyleM.TEXT_SIZE_TINY);
            editBox:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_NORMAL);
            inputEdit:setPlaceHolder(placeholder);
            editBox:setText("");
            editBox:setMaxLength(16);
            editBox:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);

            table.insert(self.editList, editBox);

            inputEdit:getParent():addChild(editBox);
        end
    end
end

-- 适配
function UIGDOpenActivity:resize()
    local node = self.node
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT底部居中并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end







